//
//  ChooseNumView.m
//  shanHaiHuiApp
//
//  Created by DS on 2018/3/14.
//  Copyright © 2018年 徐双. All rights reserved.
//

#import "ChooseNumView.h"
#import "ChooseNumTableViewCell.h"
#import "PayMentSpphModel.h"
@interface ChooseNumView()<UITableViewDelegate,UITableViewDataSource>

Copy void(^selectedHandle)(NSString *str);
Strong UITableView *tableView;

//底部视图
Strong UIButton * bottomBtn;

//头部视图
Strong UIView * headerView;
Strong UILabel * countLabel;

//尾部视图
Strong UIView * footView;
Strong UILabel * pihaoLabel;
Strong UILabel * pihaoDetalLabel;

Strong NSString * countString;

Strong NSString * countString1;


//保存购买数量
Strong NSString * buyCountStr;


@end

@implementation ChooseNumView

#pragma mark -- Action

- (void)bottomBtnClick{
    
    if ([self.buyCountStr intValue] != [self.xssl intValue]) {
        
        
        [[UIApplication sharedApplication].keyWindow makeToast:@"购买数量不匹配" duration:1 position:CSToastPositionCenter];
        
        return;
    }
    
    
    //改变数量
    
//    if (_getCompelteClick) {
//
//        _getCompelteClick(self.chooseArr,self.buyCountStr);
//    }
    
    //不改变数量
    
    if (_getCompelteClick) {
        
        _getCompelteClick(self.chooseArr);
    }
    
    [self dismiss];
    
}


#pragma init --初始化
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.alpha = 0;
        UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        backButton.backgroundColor = [UIColor blackColor];
        backButton.alpha = .6;
        backButton.titleLabel.font = DEF_FontSize_13;
        [backButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:backButton];
        [self addSubview:self.tableView];
        [self addSubview:self.bottomBtn];
        
    }
    return self;
}

#pragma mark -- 显示还是不显示
//出现
-(void)show{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}

//消失
-(void)dismiss{
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0 ;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (UITableView *)tableView{
    if (!_tableView) {
        
        if (kDevice_Is_iPhoneX) {
             _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, UNIT_HEIGHT(200), SCREEN_WIDTH, SCREEN_HEIGHT - UNIT_HEIGHT(49)- UNIT_HEIGHT(200) - height20) style:UITableViewStyleGrouped];
        }else{
           
             _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, UNIT_HEIGHT(200), SCREEN_WIDTH, SCREEN_HEIGHT - UNIT_HEIGHT(49)- UNIT_HEIGHT(200)) style:UITableViewStyleGrouped];
            
        }
        
       
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];;
        _tableView.tableHeaderView = self.headerView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.bounces = NO;
        
        
        // 添加数据刷新后，防止tableview滑动(防止reload滑动)
        
        self.tableView.estimatedRowHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.estimatedSectionFooterHeight = 0;
        
        
    }
    return _tableView;
}

- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, UNIT_HEIGHT(35))];
        
        _countLabel = [[UILabel alloc] init];
        
        
        _countLabel.font = DEF_FontSize_14;
        _countLabel.textColor = getColor(@"323232");
        
        [_headerView addSubview:_countLabel];
        [_countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
            make.right.mas_equalTo(UNIT_WIDTH(-15));
        }];
        
    }
    return _headerView;
}

- (void)setXssl:(NSString *)xssl{
    
    _xssl = xssl;

    _countLabel.text = [NSString stringWithFormat:@"购买数量: %@",xssl];
    
    self.buyCountStr = xssl;
    
    
}

-(UIView *)footView{
    if (!_footView) {
        
        
        
        _footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, UNIT_HEIGHT(35))];
        
        _pihaoLabel = [[UILabel alloc] init];
        _pihaoLabel.textColor = getColor(@"666666");
        _pihaoLabel.font = DEF_FontSize_14;
        _pihaoLabel.text = @"批号-批次";
        
        _pihaoDetalLabel = [[UILabel alloc] init];
        _pihaoDetalLabel.textColor = getColor(@"323232");
        _pihaoDetalLabel.font = DEF_FontSize_14;
        _pihaoDetalLabel.text = @"R31916-170329026";
        
        [_footView sd_addSubviews:@[_pihaoLabel,_pihaoDetalLabel]];
        
        [_pihaoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
            make.left.mas_equalTo(UNIT_WIDTH(50));
        }];
        
        [_pihaoDetalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
            make.right.mas_equalTo(UNIT_WIDTH(-16));
        }];
        
    }
    return _footView;
}

// 底部视图

- (UIButton *)bottomBtn{
    if (!_bottomBtn) {
        
        if (kDevice_Is_iPhoneX) {
            
             _bottomBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT- UNIT_HEIGHT(49) - height20, SCREEN_WIDTH, UNIT_HEIGHT(49))];
            
            
        }else{
           
             _bottomBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.tableView.frame), SCREEN_WIDTH, UNIT_HEIGHT(49))];
            
        }
        
       
        _bottomBtn.backgroundColor = [UIColor whiteColor];
        [_bottomBtn setTitle:@"完成" forState:UIControlStateNormal];
        [_bottomBtn setTitleColor:getColor(@"323232") forState:UIControlStateNormal];
        [_bottomBtn addTarget:self action:@selector(bottomBtnClick) forControlEvents:UIControlEventTouchUpInside];

    }
    return _bottomBtn;
}
 
#pragma mark - <UITableViewDelegate , UITableViewDataSource>
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return 1;
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        
        return self.chooseArr.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        ChooseNumTableViewCell * cell = [ChooseNumTableViewCell cellForTableView:tableView];
        
        cell.model = self.chooseArr[indexPath.section];
   
       // cell.textField.text = [NSString stringWithFormat:@"%d",[cell.model.xssl intValue]? : 1];
  
        cell.getPlusClick = ^(ChooseNumTableViewCell *cell,UITextField * textField) {
            
            [UIView animateWithDuration:1 animations:^{
                cell.textField.hidden = NO;
                cell.minusBtn.hidden = NO;
                
                cell.model.goodCount = [textField.text intValue];
                
                cell.model.goodCount += 1;
           
            self.countString = [NSString stringWithFormat:@"%ld",(long)cell.model.goodCount];
            cell.textField.text = self.countString;

            [self updateCountLabel];
                
                cell.model.xssl = [NSString stringWithFormat:@"%ld",(long)cell.model.goodCount];

            }];
        };
        
        cell.getMiuteClick = ^(ChooseNumTableViewCell *cell,UITextField * textField) {
            
            [UIView animateWithDuration:1 animations:^{
                
                cell.model.goodCount = [textField.text intValue];
                
                cell.model.goodCount -= 1;
                self.countString1 = [NSString stringWithFormat:@"%ld",cell.model.goodCount];
                cell.textField.text = self.countString1;
      
                if([cell.textField.text isEqualToString:@"0"]){
                    cell.textField.hidden = YES;
                    cell.minusBtn.hidden = YES;
                }
                
                [self updateCountLabel];
                
                cell.model.xssl = [NSString stringWithFormat:@"%ld",cell.model.goodCount];
                
            }];
        };
        
        cell.getTextClik = ^(ChooseNumTableViewCell *cell,UITextField * textField) {
            
           
            [UIView animateWithDuration:1 animations:^{
     
                cell.model.goodCount = [textField.text integerValue];
    
                if([cell.textField.text isEqualToString:@"0"]){
                    cell.textField.hidden = YES;
                    cell.minusBtn.hidden = YES;
                   
                }
                
                [self updateCountLabel];
                
                cell.model.xssl = [NSString stringWithFormat:@"%ld",cell.model.goodCount];
            }];
        
        };
        
        return cell;
    }


- (void)updateCountLabel{
    
    
    NSInteger count = 0;
    
    for (PayMentSpphModel * model in self.chooseArr) {
        
        count += model.goodCount;
        
    }
    
    self.buyCountStr = [NSString stringWithFormat:@"%ld",count];
    
    //_countLabel.text = [NSString stringWithFormat:@"购买数量: %@",self.buyCountStr];
    
    
    
    
    [self.tableView reloadData];
    
}


- (void)upDateChooseArr:(NSMutableArray *)arr{
    
    
    
}


    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
        return UNIT_HEIGHT(5);
    }
    

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return UNIT_HEIGHT(200);
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        
    }



- (void)setGroup:(NSArray *)group{
    
    _group = group;
    
    
    for (PayMentSpphModel * model1 in self.chooseArr) {
   
         for (SpphModel * model2 in group) {
             
             if ([model1.fPc isEqualToString:model2.fPc]) {
     
                 model1.xssl = model2.xssl;
             }
        }
    }
   
}



//- (void)setModel:(SpphModel *)model{
//
//    _model = model;
//
//
//    for (PayMentSpphModel * model1 in self.chooseArr) {
//
//        if ([model1.fPc isEqualToString:model.fPc]) {
//
//            model1.xssl = model.xssl;
//        }
//    }
//
//
//}
//


@end
